package com.small.site.controller;

import com.google.common.collect.Maps;
import com.small.site.aspect.annotation.Log;
import com.small.site.constant.Constant;
import com.small.site.pojo.SysUser;
import com.small.site.service.UserService;
import com.small.site.util.R;
import com.small.site.validation.ValidationType;
import com.small.site.vo.PageVo;
import com.small.site.vo.req.UserOwnRoleReqVo;
import com.small.site.vo.req.UserOwnRoleRespVo;
import com.small.site.vo.req.UserPageReqVo;
import com.small.site.vo.req.UserReqVo;
import com.small.site.vo.req.UserUpdateDetailInfoReqVo;
import com.small.site.vo.req.UserUpdatePasswordReqVo;
import com.small.site.vo.resp.UserRespVo;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author xiaochi
 * @date 2021/12/14 16:43
 * @desc UserController
 */
@RestController
@RequestMapping("/manager")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 获取验证码
     * @param request
     * @return
     */
    @GetMapping("/captcha")
    public R<Map<String, String>> captcha(HttpServletRequest request){
        String code = RandomStringUtils.randomAlphabetic(4);
        request.getSession().setAttribute(Constant.CAPTCHA_CODE,code);
        Map<String, String> map = Maps.newHashMap();
        map.put("code",code);
        return R.ok(map);
    }

    @Log(title = "组织管理-用户管理",action = "分页查询用户接口")
    @PreAuthorize("hasAuthority('sys:user:list')")
    @PostMapping("/users/v1")
    public R<PageVo<SysUser>> pageInfo(@RequestBody @Valid UserPageReqVo userPageReqVo){
        return R.ok(userService.pageInfo(userPageReqVo));
    }

    @PreAuthorize("hasAuthority('sys:user:add')")
    @Log(title = "组织管理-用户管理",action = "新增用户接口")
    @PostMapping("/user/v1")
    public R<String> createUser(@RequestBody @Valid UserReqVo userReqVo){
        return userService.createUser(userReqVo.buildEntity());
    }

    @Log(title = "组织管理-用户管理",action = "查询用户拥有的角色数据接口")
    @GetMapping("/user/roles/v1/{userId}")
    public R<UserOwnRoleRespVo> getUserOwnRole(@PathVariable("userId")String userId){
        return R.ok(userService.getUserOwnRole(userId));
    }

    @PreAuthorize("hasAuthority('sys:user:role:update')")
    @Log(title = "组织管理-用户管理",action = "保存用户拥有的角色信息接口")
    @PutMapping("/user/roles/v1")
    public R<String> saveUserOwnRole(@RequestBody @Valid UserOwnRoleReqVo vo){
        return userService.setUserOwnRole(vo);
    }

    @PreAuthorize("hasAuthority('sys:user:role:update')")
    @Log(title = "组织管理-重置用户密码",action = "重置用户密码接口")
    @GetMapping("/user/password/v1/{id}")
    public R<String> resetUpdatePassword(@PathVariable("id") String userId){
        return userService.resetUpdatePassword(userId);
    }

    @PreAuthorize("hasAuthority('sys:user:update')")
    @Log(title = "组织管理-用户管理",action = "列表修改用户信息接口")
    @PutMapping("/user/v1")
    public R<String> updateUserInfo(@RequestBody @Validated(ValidationType.update.class) UserReqVo userUpdateReqVo, HttpServletRequest request){
        String accessToken = request.getHeader(Constant.User.ACCESS_TOKEN);
        String userId = getUserId(accessToken);// 操作人
        if (userId == null){
            return R.error();
        }
        return userService.updateUserInfo(userUpdateReqVo,userId);
    }

    @PreAuthorize("hasAuthority('sys:user:delete')")
    @Log(title = "组织管理-用户管理",action = "批量/删除用户接口")
    @DeleteMapping("/user/v1")
    public R<String> deletedUsers(@RequestBody List<String> list, HttpServletRequest request){
        String accessToken = request.getHeader(Constant.User.ACCESS_TOKEN);
        String userId = getUserId(accessToken);// 操作人
        if (userId == null){
            return R.error();
        }
        return userService.deletedUsers(list,userId);
    }

    @Log(title = "组织管理-用户管理",action = "用户信息详情接口")
    @GetMapping("/user/info/v1")
    public R<UserRespVo> detailInfo(HttpServletRequest request){
        String accessToken = request.getHeader(Constant.User.ACCESS_TOKEN);
        String userId = getUserId(accessToken);// 操作人
        if (userId == null){
            return R.error();
        }
        return userService.detailInfo(userId);
    }

    @Log(title = "组织管理-用户管理",action = "保存个人信息接口")
    @PutMapping("/user/info/v1")
    public R<String> saveUserInfo(@RequestBody UserUpdateDetailInfoReqVo updateDetailInfoReqVo, HttpServletRequest request){
        String accessToken = request.getHeader(Constant.User.ACCESS_TOKEN);
        String userId = getUserId(accessToken);// 操作人
        if (userId == null){
            return R.error();
        }
        return userService.userUpdateDetailInfo(updateDetailInfoReqVo.buildEntity(),userId);
    }

    @Log(title = "组织管理-用户管理",action = "修改个人密码接口")
    @PutMapping("/user/password/v1")
    public R<String> updatePassword(@RequestBody @Valid UserUpdatePasswordReqVo userUpdatePasswordReqVo, HttpServletRequest request){
        String accessToken = request.getHeader(Constant.User.ACCESS_TOKEN);
        String userId = getUserId(accessToken);// 操作人
        if (userId == null){
            return R.error();
        }
        return userService.userUpdatePassword(userUpdatePasswordReqVo,userId);
    }
}
